package com.example.foo.member.service;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.member.model.MemberBankcard;
import com.example.foo.member.query.MemberBankcardQuery;
import com.example.foo.member.dao.MemberBankcardDao;

@org.springframework.stereotype.Service
public class MemberBankcardService {

	private final MemberBankcardDao memberBankcardDao;

	public MemberBankcardService(MemberBankcardDao memberBankcardDao) {
		this.memberBankcardDao = memberBankcardDao;
	}

	// ========== MemberBankcard ==========

	public PageResult<MemberBankcard> findMemberBankcards(MemberBankcardQuery memberBankcardQuery, Pageable pageable) {
		return memberBankcardDao.findMemberBankcards(memberBankcardQuery, pageable);
	}

	public List<MemberBankcard> findMemberBankcardsByIds(List<Long> ids) {
		return memberBankcardDao.selectBatchIds(ids);
	}

	public MemberBankcard findMemberBankcardById(Long id) {
		return memberBankcardDao.selectById(id);
	}

	public List<MemberBankcard> findMemberBankcardsByUserId(Long userId) {
		return memberBankcardDao.selectList(new QueryWrapper<MemberBankcard>()
				.eq("user_id", userId)
				);
	}

	public void createMemberBankcard(MemberBankcard memberBankcard) {
		memberBankcard.setCreatedAt(LocalDateTime.now());
		memberBankcardDao.insert(memberBankcard);
	}

	public void updateMemberBankcard(MemberBankcard memberBankcard) {
		memberBankcard.setUpdatedAt(LocalDateTime.now());
		memberBankcardDao.updateById(memberBankcard);
	}

	public void updateMemberBankcardState(Long id, MemberBankcard.State state, String memo) {
		MemberBankcard memberBankcard = new MemberBankcard();
		memberBankcard.setId(id);
		memberBankcard.setState(state);
		memberBankcard.setMemo(memo);
		memberBankcard.setUpdatedAt(LocalDateTime.now());
		memberBankcardDao.updateById(memberBankcard);
	}

	public void deleteMemberBankcardById(Long id) {
		memberBankcardDao.deleteById(id);
	}

}
